package com.anti.modular.bus.billwater.controller;

import cn.hutool.core.date.DateUtil;
import com.anti.commom.annotation.BusinessLog;
import com.anti.commom.annotation.Permission;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.commom.api.Result;
import com.anti.jdbc.support.page.PageParam;
import com.anti.modular.bus.billwater.param.BillWaterOnParam;
import com.anti.modular.bus.billwater.param.BillWaterParam;
import com.anti.modular.bus.billwater.result.BillWaterExcelResult;
import com.anti.modular.bus.billwater.service.BillWaterService;
import com.anti.modular.bus.housebill.entity.HouseBillExcel;
import com.anti.modular.bus.housebill.param.HouseBillParam;
import com.anti.modular.commonutils.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 账单流水控制器
 *
 * @author anti
 * @date 2021-04-24 13:44:46
 */
@RestController
@RequestMapping("billWater")
public class BillWaterController {

    @Resource
    private BillWaterService billWaterService;

    /**
     * 查询账单流水
     *
     * @author anti
     * @date 2021-04-24 13:44:46
     */
    @GetMapping("page")
    public Result page(PageParam pageParam, BillWaterOnParam billWaterParam) {
        return Result.data(billWaterService.page(pageParam,billWaterParam));
    }

    /**
     * 添加账单流水
     *
     * @author anti
     * @date 2021-04-24 13:44:46
     */
    @Permission
    @PostMapping("add")
    @BusinessLog(title = "账单流水_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public Result add(@RequestBody @Validated(BillWaterParam.add.class) BillWaterParam billWaterParam) {
            billWaterService.add(billWaterParam);
        return Result.success("新增成功");
    }

    /**
     * 删除账单流水
     *
     * @author anti
     * @date 2021-04-24 13:44:46
     */
    @Permission
    @PostMapping("delete")
    @BusinessLog(title = "账单流水_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(BillWaterParam.delete.class) BillWaterParam billWaterParam) {
            billWaterService.delete(billWaterParam);
        return Result.success("删除成功");
    }

    /**
     * 编辑账单流水
     *
     * @author anti
     * @date 2021-04-24 13:44:46
     */
    @PostMapping("edit")
    @BusinessLog(title = "账单流水_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result edit(@RequestBody @Validated(BillWaterParam.edit.class) BillWaterParam billWaterParam) {
            billWaterService.edit(billWaterParam);
        return Result.success("修改成功");
    }

    /**
     * 查看账单流水
     *
     * @author anti
     * @date 2021-04-24 13:44:46
     */
    @GetMapping("detail")
    public Result detail(@Validated(BillWaterParam.detail.class) BillWaterParam billWaterParam) {
        return Result.data(billWaterService.detail(billWaterParam));
    }
    @GetMapping("getBillWaterDetail/{id}")
    public Result getBillWaterDetail(@PathVariable(name = "id") Long  id) {
        return Result.data(billWaterService.getBillWaterDetail(id));
    }
    @GetMapping("shenhe/{id}/{type}")
    @BusinessLog(title = "账单流水_审核", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result shenhe(@PathVariable(name = "id") Long  id,@PathVariable(name = "type") Integer  type) {
        billWaterService.editShenHe(id,type);
        return Result.success("审核成功！");
    }
    /**
     * 账单流水列表
     *
     * @author anti
     * @date 2021-04-24 13:44:46
     */
    @GetMapping("list")
    public Result list(BillWaterParam billWaterParam) {
        return Result.data(billWaterService.list(billWaterParam));
    }


    @PostMapping("exportExcel")
    @BusinessLog(title = "账单流水_導出EXCEL", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void exportExcel(@RequestBody  BillWaterOnParam billWaterParam, HttpServletResponse response){
        List<BillWaterExcelResult> exportData = billWaterService.getExportData(billWaterParam);
        String title="賬單流水";
        String fileName="賬單流水"+ DateUtil.format(new Date(),"yyyy-MM-dd");
        try {
            ExcelUtils.exportExcel(exportData,title,title, BillWaterExcelResult.class, fileName, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
